package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayNewFile;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayNewFileMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayNewFileParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizGatewayNewFileResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayNewFileService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网关最新文件 服务实现类
 *
 * @author ssy
 * @date 2022/11/03 15:47
 */
@Service
public class BizGatewayNewFileServiceImpl extends ServiceImpl<BizGatewayNewFileMapper, BizGatewayNewFile> implements BizGatewayNewFileService {

    /**
     * 分页查询网关最新文件
     * @param bizGatewayNewFileParam
     * @return
     */
    @Override
    public PageResult<BizGatewayNewFile> page(BizGatewayNewFileParam bizGatewayNewFileParam) {
        // 构造条件
        LambdaQueryWrapper<BizGatewayNewFile> queryWrapper = new LambdaQueryWrapper<>();
        // 拼接查询条件-
        if (bizGatewayNewFileParam != null && ObjectUtil.isNotEmpty(bizGatewayNewFileParam.getGatewayId())) {
            queryWrapper.like(BizGatewayNewFile::getGatewayId, bizGatewayNewFileParam.getGatewayId());
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    /**
     * 添加网关最新文件
     * @param bizGatewayNewFileParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizGatewayNewFileParam bizGatewayNewFileParam) {
        // 将dto转为实体
        BizGatewayNewFile bizGatewayNewFile = new BizGatewayNewFile();
        BeanUtil.copyProperties(bizGatewayNewFileParam, bizGatewayNewFile);
        this.save(bizGatewayNewFile);
    }

    /**
     * 删除网关最新文件
     * @param bizGatewayNewFileParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayNewFileParam bizGatewayNewFileParam) {
        this.removeById(bizGatewayNewFileParam.getId());
    }

    /**
     * 编辑网关最新文件
     * @param bizGatewayNewFileParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizGatewayNewFileParam bizGatewayNewFileParam) {
        // 根据id查询实体
        BizGatewayNewFile bizGatewayNewFile = this.queryBizGatewayNewFile(bizGatewayNewFileParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayNewFileParam, bizGatewayNewFile);
        this.updateById(bizGatewayNewFile);
    }

    /**
     * 查看详情网关最新文件
     * @param bizGatewayNewFileParam
     * @return
     */
    @Override
    public BizGatewayNewFile detail(BizGatewayNewFileParam bizGatewayNewFileParam) {
        return this.queryBizGatewayNewFile(bizGatewayNewFileParam);
    }

    /**
     * 分页查询网关最新文件
     * @param bizGatewayNewFileParam
     * @return
     */
    @Override
    public PageResult<BizGatewayNewFileResult> pageList(BizGatewayNewFileParam bizGatewayNewFileParam) {
        Page<BizGatewayNewFileResult> mapPage = PageFactory.defaultPage();
        String gatewayIds = bizGatewayNewFileParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            final String[] split = gatewayIds.split(",");
            mapPage = this.baseMapper.pageList(PageFactory.defaultPage(), bizGatewayNewFileParam, split);
        }
        // 查询分页结果
        return new PageResult<>(mapPage);
    }

    /**
     * 获取网关最新文件
     * @param gatewayCode
     * @param channel
     * @return
     */
    @Override
    public BizGatewayNewFileResult getGatewayChannelNewFile(String gatewayCode, String channel) {
        return this.baseMapper.getGatewayChannelNewFile(gatewayCode,channel);
    }


    /**
     * 获取网关最新文件
     *
     * @author ssy
     * @since 2022/11/03 15:47
     */
    private BizGatewayNewFile queryBizGatewayNewFile(BizGatewayNewFileParam bizGatewayNewFileParam) {
        return this.getById(bizGatewayNewFileParam.getId());
    }
}
